<template>
  <div class="app-container">
    <el-form :model="form" :rules="rules" label-width="120px" ref="HelpCategoryForm">
      <el-form-item label="名称" prop="title">
        <el-input v-model="form.title" style="width:420px"/>
      </el-form-item>

      <el-form-item label="上级分类" prop="form.pid">
        <el-cascader
            :options="cateTree"
            v-model="default_select"
            :props="{ checkStrictly: true ,label:'title',value:'id'}"
            clearable>
        </el-cascader>
      </el-form-item>

      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" type="number" style="width:202px"/>
      </el-form-item>

      <el-form-item label="状态"  prop="status" required>
        <el-switch
          v-model="form.status"
          active-text="上线"
          inactive-text="下线">
        </el-switch>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit('HelpCategoryForm')" size="small">提 交</el-button>
        <el-button @click="onCancel()" size="small">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {createHelpCategory,updateHelpCategory,detailHelpCategory,selectTree} from '@/api/operations/helpCategory'
export default {
  components: {},
  data() {
    return {
      form: {
        title: '',
        pid:0,
        status: true,
      },
      cateTree:[],
      default_select:[0],
      rules:{
        title:[{required: true, message: '名称不能为空', trigger: 'blur' }],
      }
    }
  },
  beforeMount() {
    let id = this.$route.params.id || 0;
    this.getTree();
    if(id > 0){
      detailHelpCategory(id).then(response => {
        this.form = response.data;
        this.form.status = response.data.status == 1 ? true : false;
        this.default_select = response.data.ids
      })
    }
  },
  methods: {
    onSubmit(formName) {
     
      this.form.pid = this.default_select[this.default_select.length-1]
      this.form.status = this.form.status ? 1 : 0

      this.$refs[formName].validate((valid)=>{
        if (valid) {
          this.checkSpec()
          let id = this.$route.params.id || 0;
          if(id > 0){
            updateHelpCategory(this.form).then(response => {
              if(response.code == 200){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                setTimeout(()=>{
                  this.$router.push({name:'helpCategory'});
                }, 200)
              }
            })
          }else{
            createHelpCategory(this.form).then(response => {
              if(response.code == 200){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                setTimeout(()=>{
                  this.$router.push({name:'helpCategory'});
                }, 200)
              }
            })
          }
        }
      })
    },
    getTree(){
      if(this.cateTree.length < 1){
        selectTree().then(response=>{
          this.cateTree = response.data
        })
      }
    },
    checkSpec(){
      let _spec_name = this.form.title
      if (!_spec_name || _spec_name.length<1) {
        this.$message({
          message: '标题不能为空',
          type: 'warning'
        })
        return false;
      }

      return true;
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    },
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
.app-container {
  background: #fff;
  margin-top: 18px;
  height: 100%;
  min-height: calc(100vh - 68px);
  font-size: 12px
}

.app-container * {
  font-size: 12px;
}
</style>
